<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Infostreams extends Public_Controller
{
	
    public function __construct()
	{

		parent::__construct();
        
		$this->load->model('infostreams_m');
        $this->load->model('comments/comments_m');
        
        $this->load->helper('text');
		$this->load->helper('date');
		$this->load->helper('infostreams');
        
		$this->lang->load('infostreams');
        $this->lang->load('comments/comments');
	
        $this->load->library('form_validation');
        $this->load->library('table');
          
        date_default_timezone_set('Europe/Kiev');
        
        $this->user_id = 0;

        // Get the user ID, if it exists
		if($this->ion_auth->logged_in())
		{
			$this->user_id = $this->current_user->id;
		}

        $this->check_authorization = true;

        $this->template
			//->append_js( 'module::jquery.js' )
			//->append_css( 'module::jquery-ui.css' )
            //->append_js( 'module::jquery-ui.min.js' )
			->append_js( 'module::jquery.cookie.js' )
			//->append_metadata('<script src="http://jquery-joshbush.googlecode.com/files/jquery.maskedinput-1.2.2.min.js"></script>')
			->append_js( 'module::raphael-min.js' )
			->append_css( 'module::infostreams.css' )
            //->append_js( 'module::d3.v2.min.js' )
			->append_js( 'module::infostreams.js' );
			
	}

	// blog/page/x also routes here
	public function index()
	{
        
		// Проверяем авторизацию пользователя
		if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
        //if ($this->check_authorization AND FALSE AND (!$this->ion_auth->logged_in()))
        {
            redirect('users/login/infostreams');
        }
		
        /*
        // Validation rules
		$validation = array(
            array(
  				'field' => 'rynok_id',
  				'label' => 'Код рынка',
  				'rules' => 'xss_clean'
  			),
        );
        
        // Set the validation rules
		$this->form_validation->set_rules($validation);

        
        if ($this->form_validation->run())
		{
		      
           
        }
		else
		{
			// Return the validation error
			$this->data->error_string = $this->form_validation->error_string();
		} 
        */
        
		/*
        $where = array('InfoStreams.id >' => 0);
        if($this->input->post('date_start') !== false):
            $date_start = $this->input->post('date_start');
            //$date_start = strtotime(sprintf('%s %s:%s', $date_start, 0, 0));
            $date_start = strtotime($date_start);
            $where['InfoStreams.created_on >='] = $date_start;
        endif;
        
        if($this->input->post('date_end') !== false):
            $date_end = $this->input->post('date_end');
            //$date_end = strtotime(sprintf('%s %s:%s', $date_end, 0, 0));
            $date_end = strtotime($date_end);
            $where['InfoStreams.created_on <='] = $date_end;
        endif;
        //$this->data->InfoStreams = $this->infostreams_m->getInfoStreams($where);
        $this->data->InfoStreams = $this->infostreams_m->getPropertyValues($where);
        
		*/
		
        if ($this->input->is_ajax_request())
		{
            $status = 'respond';
            $data = array();
             
            /*    
            foreach($this->data->InfoStreams as $row):
                $data[] = array(
                    'id' => ''.$row->source_id.'-'.$row->identifier,
                    'source' => $row->source_title,
                    'title' => $row->title,
                    'text1' => $row->text1,
                    'text2' => date('Y-m-d H:i:s', $row->created_on),
                    'status' => $row->status,
                    'status_color' => ($row->status == 'create') ? '00CCFF' : '33FF00',
                    'created_on' => date('Y-m-d H:i:s', $row->created_on)
                    
                );
            endforeach;
            */
            //$status = time();
            $data = $this->data->InfoStreams;
                          
            return $this->template->build_json(array(
				'status'	 => $status,
				'message'	 => $data
			));
            
        } else {
            
            if ($this->template->layout_exists('infostreams.html'))
    		{
    			$this->template->set_layout('infostreams.html');
    		}
            
            $this->template
    			->title( lang('infostreams_infostreams_title'))
    			->set_breadcrumb( lang('infostreams_infostreams_title'))
    			->set_metadata('description', 'Модуль инфопотоков')
    			->set_metadata('keywords', 'инфо потоки')
    			->build('index', $this->data);
        }
	}
    
	//Матрица целей
	public function target_matrix()
	{
        
		// Проверяем авторизацию пользователя
		if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
        {
            redirect('users/login/infostreams/target_matrix');
        }
			
        if ($this->input->is_ajax_request())
		{
			/*
            $status = 'respond';
            $data = array();
             
            $data = $this->data->InfoStreams;
                          
            return $this->template->build_json(array(
				'status'	 => $status,
				'message'	 => $data
			));
            */
			
        } else {
            
			$this->template->append_js( 'module::target_matrix.js' );
			
            if ($this->template->layout_exists('infostreams.html'))
    		{
    			$this->template->set_layout('infostreams.html');
    		}
            
            $this->template
    			->title( lang('infostreams_infostreams_target_matrix_title'))
    			->set_breadcrumb( lang('infostreams_infostreams_target_matrix_title'))
    			->set_metadata('description', lang('infostreams_infostreams_target_matrix_title'))
    			->set_metadata('keywords', lang('infostreams_infostreams_target_matrix_title'))
    			->build('target_matrix', $this->data);
        }
	}
	
    public function Tree()
	{
        
        if ($this->input->is_ajax_request())
		{
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $parent_id = $this->input->post('id');
            
            if(($parent_id === false) or (!is_numeric($parent_id))){
                $parent_id = 0;
            }
            
            $parent_id = $parent_id + 0;

            $status = 'ok';
            $data = $this->infostreams_m->get_InfostreemTree($parent_id);
            
            return $this->template->build_json(array(
				'status'	 => $status,
				'message'	 => $data
			));
            
        } else {
            
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'Tree';
        }     
    }
	
	public function GetInfostreams()
	{
        
        if ($this->input->is_ajax_request())
		{
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $data = $this->infostreams_m->get_Infostreams();
            
            return $this->template->build_json(array(
				'status'	 => $status,
				'message'	 => $data
			));
            
        } else {
            
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'GetInfostreams';
        }     
    }
	
    public function Add()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
            
            $post = $this->input->post(NULL, true);
            $parent_id = $this->input->post('parent');
            //$version = $this->input->post('version');
            $nested_level = $this->input->post('nested_level');
            $post['version'] = 1;
            if(($parent_id === false) or (!is_numeric($parent_id))){
                $parent_id = 0;
                $post['parent'] = $parent_id;
            }
            
            $parent_id = $parent_id + 0;
            $post['level'] = $this->infostreams_m->getLevelParent($parent_id);
            if(($nested_level === true) OR ($nested_level === 'true')){
                $post['level']++;    
            }
            
            $last_id = $this->infostreams_m->Add($post);
            		
            if($last_id > 0){

                //$users = $this->ion_auth->get_active_users();
                //foreach ($users as $row) {
                //    $input = array(
                //        'module' => 'streams:infostream',
                //        'module_id' => $last_id,
                //        'user_id' => $row->id
                //        );
                //    $last_id_notice = $this->infostreams_m->AddNotice($input);
                //}

                $status = 'ok';
                $data = array('id' => $last_id);
            } else {
                $status = 'error';
                $data = array('id' => $last_id);
            }
            //$data = $this->infostreams_m->InfostreemAdd($parent_id);
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'Add';
        }     
    }

    public function Edit()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            //if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            if ( (!$this->ion_auth->logged_in()) )
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $post = $this->input->post(NULL, true);
            $id = $this->input->post('id');
            $version = $this->input->post('version');
            $post['version'] = $version + 1;
            
            if(($id === false) or (!is_numeric($id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "id"'
                ));
            }
            
            $check_user = $this->infostreams_m->check_user($id);
            if(($check_user === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not edit object! you need to be responsible!'
                ));
            }

            if(($version === false) or (!is_numeric($version))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "version"'
                ));
            }
            
            $check_version = $this->infostreams_m->check_version($id, $version);
            if(($check_version === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not edit! version of object is change!'
                ));
            }

            $status = 'ok';
            $data = $this->infostreams_m->update($id, $post);
            if($data == false){
                $status = 'error';
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'Edit';
        }     
    }

    public function Delete()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $id = $this->input->post('id');
            
            if(($id === false) or (!is_numeric($id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "id"'
                ));
            }
            
            $id = $id + 0;

            $check_user = $this->infostreams_m->check_user($id);
            if(($check_user === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not delete object! you need to be responsible!'
                ));
            }

            $data = $this->infostreams_m->get_InfostreemTree($id);
            if($data or (is_array($data) and sizeof($data) > 0)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'you can not remove the object, as contains embedded data!'
                ));
            }

            $status = 'ok';
            $data = $this->infostreams_m->delete($id);
            if($data == false){
                $status = 'error';
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'Delete';
        }     
    }

    public function GetEvents()
    {
        
        if ($this->input->is_ajax_request())
        {
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $infostream_id = $this->input->post('infostream_id');
            
            if(($infostream_id === false) or (!is_numeric($infostream_id))){
                $infostream_id = 0;
            }
            
            $infostream_id = $infostream_id + 0;

            $status = 'ok';
            $data = $this->infostreams_m->get_Events($infostream_id);
            
            foreach ($data as &$event)
            {
                $event->_count_comments = 1;
                $event->_count_notices = 0;
                    
                $reference = 'infostreams';
                $ref_id = $event->id;
            
                $comments = $this->comments_m->get_by_module_item($reference, $ref_id);
                $event->_count_comments = sizeof($comments);
                
                unset($comments);

                $event->_contains_notice = true;
            }

            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {
            
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'GetEvents';
        }     
    }

    public function AddEvent()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
            
            $post = $this->input->post(NULL, true);
            $infostream_id = $this->input->post('infostream_id');
            $post['version'] = 1;
            if(($infostream_id === false) or (!is_numeric($infostream_id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "infostream_id"'
                ));
                //$infostream_id = 0;
                //$post['infostream_id'] = $infostream_id;
            }
            
            $infostream_id = $infostream_id + 0;
            
            $last_id = $this->infostreams_m->AddEvent($post);
                    
            if($last_id > 0){
                
                //$users = $this->ion_auth->get_active_users();
                //foreach ($users as $row) {
                //    $input = array(
                //        'module' => 'streams:events',
                //        'module_id' => $last_id,
                //        'user_id' => $row->id
                //        );
                //    $last_id_notice = $this->infostreams_m->AddNotice($input);
                //}

                $status = 'ok';
                $data = array('id' => $last_id);
            } else {
                $status = 'error';
                $data = array('id' => $last_id);
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            echo 'AddEvent';
        }     
    }

    public function EditEvent()
	{
        
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $post = $this->input->post(NULL, true);
            $id = $this->input->post('id');
            $version = $this->input->post('version');
            $post['version'] = $version + 1;
            
            if(($id === false) or (!is_numeric($id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "id"'
                ));
            }
            
            $check_user = $this->infostreams_m->check_user_events($id);
            if(($check_user === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not edit event! you need to be responsible!'
                ));
            }

            if(($version === false) or (!is_numeric($version))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "version"'
                ));
            }
            
            $check_version = $this->infostreams_m->check_version_event($id, $version);
            if(($check_version === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not edit! version of object is change!'
                ));
            }

            $status = 'ok';
            $data = $this->infostreams_m->UpdateEvent($id, $post);
            if($data == false){
                $status = 'error';
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            echo 'EditEvent';
        }     
    }

    public function DeleteEvent()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $id = $this->input->post('id');
            
            if(($id === false) or (!is_numeric($id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "id"'
                ));
            }
            
            $id = $id + 0;

            $check_user = $this->infostreams_m->check_user_events($id);
            if(($check_user === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not delete event! you need to be responsible!'
                ));
            }

            $status = 'ok';
            $data = $this->infostreams_m->DeleteEvent($id);
            if($data == false){
                $status = 'error';
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'DeleteEvent';
        }     
    }
     
	public function GetComments()
    {
        
        if ($this->input->is_ajax_request())
        {
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $events_id = $this->input->post('events_id');
            
            if(($events_id === false) or (!is_numeric($events_id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "events_id"'
                ));
            }
            
            $events_id = $events_id + 0;

            $status = 'ok';
            //$data = $this->comments_m->get_Events($events_id);
            
			//$reference = 'events';
			$reference = 'infostreams';
            $ref_id = $events_id;
			
			$comments = $this->comments_m->get_by_module_item($reference, $ref_id);
	
			// loop through the comments and escape {{ foo }} and html tags
			foreach ($comments as &$comment)
			{
				// Override specified website if they are a user
				if ($comment->website and $comment->user_id )//and Settings::get('enable_profiles'))
				{
					$comment->website = 'user/'.$comment->user_id;
				}

                $comment->_contains_notice = true;
                $comment->created_on = date('d.m.Y H:i:s', $comment->created_on);
                $comment->name = anchor('user/'.$comment->user_id, $comment->name, 'target="_blank"');

                $comment->date_notice = null;
                $comment->notice_came = false;
                $where = array(
                    'module' => 'comments',
                    'module_id' => $comment->id,
                    'type_notice' => 'to_date',
                    'user_id' => $comment->user_id
                    );
                $notices = $this->infostreams_m->getNotices($where);
                if(sizeof($notices) > 0):
                    $comment->date_notice = $notices[0]->date_notice;
                    if($notices[0]->date_notice <= date('Y-m-d H:m:s', time())):
                        $comment->notice_came = true;
                    endif;
                endif; 

                //Признак редактирования ответственным
                $comment->_is_creator = false;
                if( $this->ion_auth->logged_in() AND ($comment->user_id == $this->user_id) )
                {
                    $comment->_is_creator = true;    
                }
                    
				foreach ($comment as &$body)
				{
					$body = escape_tags($body);
				}
			}
            //process_comment_items($comments);
            
			$data = $comments;
			
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {
            
            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            echo 'GetComments';
        }     
    }
    
    public function AddComment()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
            
            $post = $this->input->post(NULL, true);
            $events_id = $this->input->post('events_id');
            
            if(($events_id === false) or (!is_numeric($events_id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "events_id"'
                ));
            }
            
            $events_id = $events_id + 0;
            
            if ($this->ion_auth->logged_in())
            {
                $post['user_id'] = $this->user_id;
                $post['name'] = $this->current_user->display_name;
                $post['email'] = $this->current_user->email;
                $post['website'] = 'user/'.$this->user_id;
            }
            $post['is_active'] = 1;
            $post['module'] = 'infostreams';
            $post['module_id'] = $events_id;
            
            $last_id = $this->comments_m->insert($post);
                    
            if($last_id > 0){

                //$users = $this->ion_auth->get_active_users();
                //foreach ($users as $row) {
                //    $input = array(
                //        'module' => 'comments',
                //        'module_id' => $last_id,
                //        'user_id' => $row->id
                //        );
                //    $last_id_notice = $this->infostreams_m->AddNotice($input);
                //}
                if($this->input->post('date_notice') !== false): 
                    $input = array(
                        'module' => 'comments',
                        'module_id' => $last_id,
                        'date_notice' => $this->input->post('date_notice'),
                        'type_notice' => 'to_date',
                        'user_id' => $this->user_id
                        );
                    $last_id_notice = $this->infostreams_m->AddNotice($input);
                endif;

                $status = 'ok';
                //$data = array('id' => $last_id);
                $data = $this->comments_m->get($last_id);
                $data->created_on = date('d.m.Y H:i:s', $data->created_on);
                // work out who did the commenting
                if ($data->user_id > 0)
                {
                    $data->name = anchor('user/'.$data->user_id, $data->name);
                }
                $data->date_notice = null;
                $data->notice_came = false;
                $where = array(
                    'module' => 'comments',
                    'module_id' => $last_id,
                    'type_notice' => 'to_date',
                    'user_id' => $data->user_id
                    );
                $notices = $this->infostreams_m->getNotices($where);
                if(sizeof($notices) > 0):
                    $data->date_notice = $notices[0]->date_notice;
                    if($notices[0]->date_notice <= date('Y-m-d H:m:s', time())):
                        $data->notice_came = true;
                    endif;
                endif; 
                
            } else {
                $status = 'error';
                $data = array('id' => $last_id);
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            echo 'AddComment';
        }     
    }

    public function EditComment()
    {
        
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $post = $this->input->post(NULL, true);
            $id = $this->input->post('id');
            
            if(($id === false) or (!is_numeric($id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "id"'
                ));
            }
            
            $check_user = $this->infostreams_m->check_user_comments($id);
            if(($check_user === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not edit comment! you need to be responsible!'
                ));
            }

            $status = 'ok';
            $data = $this->comments_m->update($id, $post);
            if($data == false){
                $status = 'error';
            }
            
            if($this->input->post('date_notice') !== fasle): 
                $input = array(
                    'module' => 'comments',
                    'module_id' => $id,
                    'date_notice' => $this->input->post('date_notice'),
                    'type_notice' => 'to_date',
                    'user_id' => $this->user_id
                    );
                $last_id_notice = $this->infostreams_m->AddNotice($input);
            endif;

            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'EditComment';
        }     
    }

    public function DeleteComment()
    {
        
        if ($this->input->is_ajax_request())
        {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'Not authorize!'
                ));
            }
        
            $id = $this->input->post('id');
            
            if(($id === false) or (!is_numeric($id))){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'fill in the required parameter "id"'
                ));
            }
            
            $id = $id + 0;

            $check_user = $this->infostreams_m->check_user_comments($id);
            if(($check_user === false)){
                return $this->template->build_json(array(
                    'status'     => 'error',
                    'message'    => 'error! you can not delete comment! you need to be responsible!'
                ));
            }

            $status = 'ok';
            $data = $this->infostreams_m->DeleteComment($id);
            if($data == false){
                $status = 'error';
            }
            
            return $this->template->build_json(array(
                'status'     => $status,
                'message'    => $data
            ));
            
        } else {

            // Проверяем авторизацию пользователя
            if ($this->check_authorization AND (!$this->ion_auth->logged_in()))
            {
              redirect('users/login/infostreams');
              show_error('error. Not authorize!');
            }

            //show_error('error. Only Ajax request!');
            echo 'DeleteComment';
        }     
    }
        
}

?>